﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using libtcod;
using Threshold.MapGenerators;
using Threshold.Entities;
using Threshold.UserInterface;

namespace Threshold
{
	class Game
	{
		private const int KEY_PRESS_FLAGS = (int)TCODKeyStatus.KeyPressed + (int)TCODKeyStatus.KeyReleased;

		private bool _isRunning;

		Map _map;
		Player _player;
		Plant _plant;
		//Time _time;

		public Game()
		{
			_map = new FloorGenerator().Generate(80, 50);
			_player = new Player();
			_plant = new Plant();
			//_time = new Time();
		}

		public void Loop()
		{
			TCODKey key;
			TCODMouseData mouse;

			_isRunning = true;

			while (!TCODConsole.isWindowClosed() && _isRunning)
			{
				key = TCODConsole.checkForKeypress(KEY_PRESS_FLAGS);
				mouse = TCODMouse.getStatus();

				Update(key, mouse, TCODSystem.getLastFrameLength());
				Draw(TCODConsole.root);
			}
		}

		private void Update(TCODKey key, TCODMouseData mouse, float lastFrameLength)
		{
			if (key.Pressed)
			{
				int deltaX = 0;
				int deltaY = 0;

				if (key.Pressed)
				{
					switch (key.KeyCode)
					{
						case TCODKeyCode.KeypadOne:
							deltaX = -1;
							deltaY = 1;
							break;
						case TCODKeyCode.KeypadTwo:
							deltaY = 1;
							break;
						case TCODKeyCode.KeypadThree:
							deltaX = 1;
							deltaY = 1;
							break;
						case TCODKeyCode.KeypadFour:
							deltaX = -1;
							break;
						case TCODKeyCode.KeypadSix:
							deltaX = 1;
							break;
						case TCODKeyCode.KeypadSeven:
							deltaX = -1;
							deltaY = -1;
							break;
						case TCODKeyCode.KeypadEight:
							deltaY = -1;
							break;
						case TCODKeyCode.KeypadNine:
							deltaX = 1;
							deltaY = -1;
							break;
					}
				}
				else // key was released
				{
				}

				_player.X += deltaX;
				_player.Y += deltaY;

				_player.X = _player.X.Clamp(0, _map.Width - 1);
				_player.Y = _player.Y.Clamp(0, _map.Height - 1);
			}
			else // key was released
			{
				if (key.KeyCode == TCODKeyCode.Escape)
				{
					key.KeyCode = TCODKeyCode.NoKey; // consume this...
					_isRunning = false;
				}
				if (key.Character == 'l' && key.LeftControl)
				{
					key.KeyCode = TCODKeyCode.NoKey;
					_map.Load();
				}
				if (key.Character == 's' && key.LeftControl)
				{
					key.KeyCode = TCODKeyCode.NoKey;
					_map.Save();
				}
				if (key.KeyCode == TCODKeyCode.F11)
				{
					key.KeyCode = TCODKeyCode.NoKey; // consume this...
					IMapGenerator mapGenerator = new DungeonGenerator();
					_map = mapGenerator.Generate(80, 50);
				}
				if (key.KeyCode == TCODKeyCode.F12)
				{
					key.KeyCode = TCODKeyCode.NoKey; // consume this...
					IMapGenerator mapGenerator = new CaveGenerator();
					_map = mapGenerator.Generate(80, 50);
				}
			}

			_map.Update(key, mouse, lastFrameLength);
			_player.Update(key, mouse, lastFrameLength);
			_plant.Update(key, mouse, lastFrameLength);
			
			//_time.Update(key, mouse, lastFrameLength);
		}

		private void Draw(TCODConsole console)
		{
			console.clear();

			_map.Draw(console);
			_plant.Draw(console);
			_player.Draw(console);

			//_time.Draw(console);

			console.printEx(79, 49, TCODBackgroundFlag.None, TCODAlignment.RightAlignment, String.Format("{0} fps", TCODSystem.getFps()));
			TCODConsole.flush();
		}

	}
}
